package com.zzu.team.controller;

import com.zzu.team.entity.Result;
import com.zzu.team.entity.ResultVo;
import com.zzu.team.entity.ReturnBean;
import com.zzu.team.entity.Tester;
import com.zzu.team.service.ResultService;
import com.zzu.team.service.TesterService;
import com.zzu.team.util.Constants;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("result")
public class ResultController extends BaseController{

    @Resource
    ResultService resultService;

    @Resource
    TesterService testerService;

    // 根据主键id删除
//    int deleteByPrimaryKey(Integer id){};

    // 批量删除  根据主键id批量删除
//    int deleteBatchByPrimaryKey(List<Integer> ids);

    // 批量删除  根据用户id tester_id批量删除
//    int deleteBatchByTesterId( List<Integer> testerIds);

    // 批量删除  根据问题id question_id批量删除
//    int deleteBatchByQuestionId(List<Integer> questionIds);

    // 单个插入
//    int insert(Result record);

//    int insertSelective(Result record);

    // 批量插入
    @PostMapping("insertBatch")
    public ReturnBean insertBatch(@RequestBody Collection<Result> result){
        for (Result r: result){
            System.out.println(r);
        }
        int i = resultService.insertBatch((List<Result>) result);

        if (i>0){
            return super.success("提交成功");
        } else{
            return fail("提交失败");
        }
    }


    // 根据主键查询
//    Result selectByPrimaryKey(Integer id);

    // 根据用户id查询
//    List<Result> selectByTesterId(Integer testerId);

    // 根据问题id查询
//    List<Result> selectByQuestionId(Integer questionId);


//    int updateByPrimaryKeySelective(Result record);

//    int updateByPrimaryKey(Result record);


    // 查询 所有用户的最终测试结果
    @RequestMapping("selectAllTesterResult")
    public ReturnBean selectAllTesterResult(Integer page, Integer limit, Tester tester){
        if (page == null){
            page = Constants.page;
            limit = Constants.limit;
        }
        if (tester!=null){
            System.out.println("result:"+tester.toString());
        }
        List<ResultVo> resultVos = resultService.selectAllTesterResult(page, limit, tester);

        return success(resultVos, resultService.count(tester));
    }

    // 查找某个用户的测试结果
    @RequestMapping("selectResultByTesterId")
    public ReturnBean selectResultByTesterId(@RequestParam("testerId") Integer id){

        Tester tester = testerService.getById(id);
        List<ResultVo> resultVos = resultService.selectAllTesterResult(Constants.page,Constants.limit, tester);

        System.out.println(resultVos.size());
        return success(resultVos.get(0));
    }

    // 查询符合条件的用户个数
//    long count(Tester tester);

}
